package service

import (
	"context"
	"insighttracking/query/dao"
	"insighttracking/query/model"
)

type _storeSortingDetailService struct {
}

func (s _storeSortingDetailService) CreateBatchApi(ctx context.Context, tx *dao.QueryTx, detail []*model.StoreSortingDetailEntity) error {
	if err := tx.WithContext(ctx).StoreSortingDetailEntity.CreateInBatches(detail, len(detail)); err != nil {
		return err
	}
	return nil
}

func (s _storeSortingDetailService) GetStoreSortingDetailApi(ctx context.Context, id int64) ([]*model.StoreSortingDetailEntity, error) {
	return dao.StoreSortingDetailEntity.WithContext(ctx).Where(dao.StoreSortingDetailEntity.StoreSortingID.Eq(id)).Find()
}

func (s _storeSortingDetailService) GetStoreSortingDetailByStoreSortingIdListApi(ctx context.Context, idList []int64) ([]*model.StoreSortingDetailEntity, error) {
	return dao.StoreSortingDetailEntity.WithContext(ctx).Where(dao.StoreSortingDetailEntity.StoreSortingID.In(idList...)).Find()
}

func (s _storeSortingDetailService) GetStoreSortingDetailByOrdersDetailIdListApi(ctx context.Context, list []int64) ([]*model.StoreSortingDetailEntity, error) {
	return dao.StoreSortingDetailEntity.WithContext(ctx).Where(dao.StoreSortingDetailEntity.OrdersDetailID.In(list...)).Find()
}

func (s _storeSortingDetailService) GetStoreSortingDetailByOrdersDetailApi(ctx context.Context, orderDetailId int64) (*model.StoreSortingDetailEntity, error) {
	return dao.StoreSortingDetailEntity.WithContext(ctx).Where(dao.StoreSortingDetailEntity.OrdersDetailID.Eq(orderDetailId)).First()
}

func (s _storeSortingDetailService) GetStoreSortingDetailByOrdersDetailListApi(ctx context.Context, orderDetailIdList []int64) ([]*model.StoreSortingDetailEntity, error) {
	return dao.StoreSortingDetailEntity.WithContext(ctx).Where(dao.StoreSortingDetailEntity.OrdersDetailID.In(orderDetailIdList...)).Find()
}
func (s _storeSortingDetailService) GetStoreSortingDetailByOrdersDetailIdApi(ctx context.Context, ordersDetailId, spuId int64) (*model.StoreSortingDetailEntity, error) {
	return dao.StoreSortingDetailEntity.WithContext(ctx).Where(dao.StoreSortingDetailEntity.SpuID.Eq(spuId), dao.StoreSortingDetailEntity.OrdersDetailID.Eq(ordersDetailId)).First()
}

func (s _storeSortingDetailService) GetStoreSortingDetailByOrdersDetailIdsApi(ctx context.Context, ordersDetailId int64) ([]*model.StoreSortingDetailEntity, error) {
	return dao.StoreSortingDetailEntity.WithContext(ctx).Where(dao.StoreSortingDetailEntity.OrdersDetailID.Eq(ordersDetailId)).Find()
}

func (s _storeSortingDetailService) DeleteByIdApi(ctx context.Context, tx *dao.QueryTx, id int64) error {
	if _, err := tx.StoreSortingDetailEntity.WithContext(ctx).Where(dao.StoreSortingDetailEntity.ID.Eq(id)).Delete(); err != nil {
		return err
	}
	return nil
}

func (s _storeSortingDetailService) GetStoreSortingDetailByUuIdApi(ctx context.Context, uuid string) (*model.StoreSortingDetailEntity, error) {
	return dao.StoreSortingDetailEntity.WithContext(ctx).Where(dao.StoreSortingDetailEntity.UUID.Eq(uuid)).First()
}

func (s _storeSortingDetailService) GetStoreSortingDetailBySpuIDApi(ctx context.Context, orderID, spuID int64) ([]*model.StoreSortingDetailEntity, error) {
	return dao.StoreSortingDetailEntity.WithContext(ctx).
		LeftJoin(dao.StoreSortingEntity, dao.StoreSortingEntity.ID.EqCol(dao.StoreSortingDetailEntity.StoreSortingID)).
		Where(dao.StoreSortingDetailEntity.SpuID.Eq(spuID), dao.StoreSortingEntity.OrdersID.Eq(orderID)).Find()
}

func (s _storeSortingDetailService) GetStoreSortingDetailByProductionPlanApi(ctx context.Context, productionPlanID, spuID int64) ([]*model.StoreSortingDetailEntity, error) {
	return dao.StoreSortingDetailEntity.WithContext(ctx).
		Where(dao.StoreSortingDetailEntity.SpuID.Eq(spuID), dao.StoreSortingDetailEntity.ProductionDetailID.Eq(productionPlanID)).Find()
}

var StoreSortingDetailService = new(_storeSortingDetailService)
